const base64 = content => window.btoa(unescape(encodeURIComponent(content)));
const tableToExcel = (table, fileName)=>{
    let excelContent = table.innerHTML;
    let excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
    excelFile += "<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head>";
    excelFile += "<body><table>";
    excelFile += excelContent;
    excelFile += "</table></body>";
    excelFile += "</html>";
    let link = "data:application/vnd.ms-excel;base64," + base64(excelFile);
    let a = document.createElement("a");
    a.download = fileName + ".xlsx";
    a.href = link;
    a.click();
}
const exportExcel = (items, name) => {
    const table = document.createElement('table')
    const thead = document.createElement('thead')
    const thead_tr = document.createElement('tr');
    thead.appendChild(thead_tr);
    const titles = Object.keys(items[0]);
    const fields = Object.keys(items[0]);
    titles.map(title => {
        const th = document.createElement('th');
        th.innerText = title
        thead_tr.appendChild(th);
    });
    const tbody = document.createElement('tbody');
    items.map(item => {
        const tbody_tr = document.createElement('tr');
        fields.map(field => {
            const td = document.createElement('td');
            td.innerText = item[field];
            tbody_tr.appendChild(td);
        });
        tbody.appendChild(tbody_tr);
    });
    table.appendChild(thead);
    table.appendChild(tbody);
    console.log({table, items, titles, fields})
    tableToExcel(table, name);
};